package cn.lyz.craft.web.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.lyz.craft.common.R;
import cn.lyz.craft.service.entity.User;
import cn.lyz.craft.service.service.UserService;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @GetMapping
    public R<List<User>> getAllUsers() {
        return R.success(userService.findAll());
    }

    @GetMapping("/{id}")
    public R<User> getUserById(@PathVariable Long id) {
        return R.success(userService.findById(id));
    }

    @PostMapping
    public R<User> createUser(@RequestBody User user) {
        return R.success(userService.save(user));
    }

    @PutMapping("/{id}")
    public R<User> updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return R.success(userService.save(user));
    }

    @DeleteMapping("/{id}")
    public R<?> deleteUser(@PathVariable Long id) {
        userService.deleteById(id);
        return R.success();
    }
}